package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetQueryAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型(MIME) https://www.runoob.com/http/mime-types.html
        resp.setContentType("text/html;charset=utf-8");
        //获取输出流主要用于向网页中显示信息
        PrintWriter out = resp.getWriter();
        //调用Service中的方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Guestbook> list = service.getAll();
        out.println("<table border='1'>");
        out.println("<tr>");
        out.println("<td>id</td>");
        out.println("<td>name</td>");
        out.println("<td>pass</td>");
        out.println("<td>phone</td>");
        out.println("<td>email</td>");
        out.println("<td>title</td>");
        out.println("<td>content</td>");
        out.println("<td>createdtime</td>");
        out.println(" </tr>");
        for (int i = 0;i<list.size();i++) {
            Guestbook gb = list.get(i);
            out.println("<tr>");
            out.println("<td>"+gb.getId()+"</td>");
            out.println("<td>"+gb.getName()+"</td>");
            out.println("<td>"+gb.getPass()+"</td>");
            out.println("<td>"+gb.getPhone()+"</td>");
            out.println("<td>"+gb.getEmail()+"</td>");
            out.println("<td>"+gb.getTitle()+"</td>");
            out.println("<td>"+gb.getContent()+"</td>");
            out.println("<td>"+gb.getCreatedtime()+"</td>");
            out.println(" </tr>");
        }
        out.println("</table>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
